home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / api / register.js < prev    next >
Text File  |  2008-03-30  |  4KB  |  162 lines

  1. /*
  2.     api/register.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. const WOT_REGISTER_RUNNING = "wot_register_running";
  8.  
  9. var wot_api_register =
  10. {
  11.     init: function()
  12.     {
  13.         this.ready = false;
  14.         this.timeout = null;
  15.         this.tries = 0;
  16.     },
  17.  
  18.     geteid: function()
  19.     {
  20.         try {
  21.             if (wot_prefs.extension_id && wot_prefs.extension_id.length > 0) {
  22.                 return true;
  23.             }
  24.             if (!wot_prefs.setChar("extension_id", wot_crypto.nonce())) {
  25.                 return false;
  26.             }
  27.             return (wot_prefs.extension_id.length > 0);
  28.         } catch (e) {
  29.             dump("wot_api_register.geteid: failed with " + e + "\n");
  30.         }
  31.         return false;
  32.     },
  33.  
  34.     send: function()
  35.     {
  36.         try {
  37.             if (this.ready) {
  38.                 return;
  39.             }
  40.  
  41.             if (this.timeout) {
  42.                 window.clearTimeout(this.timeout);
  43.                 this.timeout = null;
  44.             }
  45.  
  46.             if (wot_prefs.witness_id &&
  47.                 wot_prefs.witness_id.length  == WOT_LENGTH_WITNESS_ID &&
  48.                 wot_prefs.witness_key &&
  49.                 wot_prefs.witness_key.length == WOT_LENGTH_WITNESS_KEY) {
  50.                 this.ready = true;
  51.                 wot_core.update();
  52.                 return;
  53.             }
  54.  
  55.             if (wot_browser.isoffline()) {
  56.                 wot_status.set("offline",
  57.                     wot_util.getstring("description_offline"));
  58.                 this.timeout = window.setTimeout(wot_api_register.send,
  59.                     WOT_INTERVAL_REGISTER_OFFLINE);
  60.                 return;
  61.             }
  62.  
  63.             wot_status.set("notready",
  64.                 wot_util.getstring("description_notready"));
  65.  
  66.             if (!this.geteid() ||
  67.                     wot_hashtable.get(WOT_REGISTER_RUNNING)) {
  68.                 this.timeout = window.setTimeout(wot_api_register.send,
  69.                     WOT_INTERVAL_REGISTER_ERROR);
  70.                 return;
  71.             }
  72.  
  73.             wot_hashtable.set(WOT_REGISTER_RUNNING, 1);
  74.             ++this.tries;
  75.  
  76.             var request = new XMLHttpRequest();
  77.  
  78.             request.open("GET", WOT_SERVICE_SECURE +
  79.                 WOT_SERVICE_API_REGISTER +
  80.                 "?nonce="    + wot_crypto.nonce() +
  81.                 "&eid="        + wot_prefs.extension_id +
  82.                 "&lang="    + wot_util.getstring("language") +
  83.                 "&version="    + WOT_PLATFORM + "-" + WOT_VERSION);
  84.  
  85.             new wot_cookie_remover(request);
  86.  
  87.             request.onload = this.onload;
  88.             request.send(null);
  89.         } catch (e) {
  90.             dump("wot_register.send: failed with " + e + "\n");
  91.             this.error();
  92.         }
  93.     },
  94.  
  95.     onload: function(event)
  96.     {
  97.         try {
  98.             if (!event || !event.target || event.target.status != 200 ||
  99.                     !event.target.responseXML) {
  100.                 wot_api_register.error();
  101.                 return;
  102.             }
  103.  
  104.             var reg = null;
  105.             var tags = event.target.responseXML.getElementsByTagName(
  106.                             WOT_SERVICE_XML_REGISTER);
  107.  
  108.             if (tags) {
  109.                 reg = tags.item(0);
  110.             }
  111.  
  112.             if (!reg || !reg.attributes) {
  113.                 wot_api_register.error();
  114.                 return;
  115.             }
  116.  
  117.             var id  = reg.attributes.getNamedItem(WOT_SERVICE_XML_REGISTER_ID);
  118.             var key = reg.attributes.getNamedItem(WOT_SERVICE_XML_REGISTER_KEY);
  119.  
  120.             if (!id || !id.nodeValue || !key || !key.nodeValue ||
  121.                 id.nodeValue.length  != WOT_LENGTH_WITNESS_ID ||
  122.                 key.nodeValue.length != WOT_LENGTH_WITNESS_KEY) {
  123.                 wot_api_register.error();
  124.                 return
  125.             }
  126.  
  127.             if (!wot_prefs.setChar("witness_id", id.nodeValue) ||
  128.                 !wot_prefs.setChar("witness_key", key.nodeValue)) {
  129.                 wot_api_register.error();
  130.                 return;
  131.             }
  132.  
  133.             wot_api_register.ready = true;
  134.             wot_my_session.update(true);
  135.             wot_core.update();
  136.  
  137.             wot_hashtable.remove(WOT_REGISTER_RUNNING);
  138.         } catch (e) {
  139.             dump("wot_register.onload: failed with " + e + "\n");
  140.             wot_api_register.error();
  141.         }
  142.     },
  143.  
  144.     error: function()
  145.     {
  146.         try {
  147.             wot_status.set("error",
  148.                 wot_util.getstring("description_error_register"));
  149.  
  150.             wot_api_register.timeout =
  151.                 window.setTimeout(wot_api_register.send,
  152.                     wot_api_register.tries * WOT_INTERVAL_REGISTER_ERROR);
  153.  
  154.             wot_hashtable.remove(WOT_REGISTER_RUNNING);
  155.         } catch (e) {
  156.             dump("wot_register.error: failed with " + e + "\n");
  157.         }
  158.     }
  159. };
  160.  
  161. wot_api_register.init();
  162.